<?php
class DBConnector_PDO {
	private static $instance;
	private $user;
	private $passwd;
	private $dataBase;
	private $conn;
	private function __construct(SimpleXMLElement $dbConfig) {
		try {
			$this->user = (string) $dbConfig->pdo->user;
			$this->passwd = (string) $dbConfig->pdo->password;
			$this->dsn = (string) $dbConfig->pdo->dsn;
			$this->conn = new PDO($this->dsn, $this->user, $this->passwd, array(PDO::ATTR_PERSISTENT => true,PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function __destruct() {
		$this->conn = null;
	}
	public static function init(SimpleXMLElement $dbConfig) {
		if (!isset(self::$instance)) {
			$cls = __CLASS__;
			self::$instance = new $cls($dbConfig);
		}
		return self::$instance;
	}
	public function beginTransaction() {
		try {
			$this->conn->beginTransaction();
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function commit() {
		try {
			$this->conn->commit();
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function rollback() {
		try {
			$this->conn->rollback();
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function exec($sql) {
		try {
			return $this->conn->exec($sql);
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function query($sql) {
		try {
			return $this->conn->query($sql);
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
	public function prepare($sql) {
		try {
			return $this->conn->prepare($sql);
		} catch (PDOException $e) {
			print "PDO Exception: ".$e->getMessage()."<br/>";
			die();
		}
	}
}
class DBConnector_DB2 {
	private static $instance;
	private $user;
	private $passwd;
	private $dataBase;
	private $conn;
	private $pstmt;
	private function __construct(SimpleXMLElement $dbConfig) {
		$this->user = (string) $dbConfig->user;
		$this->passwd = (string) $dbConfig->password;
		$this->dataBase = (string) $dbConfig->database;
		$this->conn = db2_connect($this->dataBase, $this->user, $this->passwd);
		if ($this->conn) {
			db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF);
		}
	}
	public function __destruct() {
		if ($this->conn) {
			db2_close($this->conn);
		}
	}
	public static function init(SimpleXMLElement $dbConfig) {
		if (!isset(self::$instance)) {
			$cls = __CLASS__;
			self::$instance = new $cls($dbConfig);
		}
		return self::$instance;
	}
	public function commit() {
		if ($this->conn) {
			db2_commit($this->conn);
		}
	}
	public function rollback() {
		if ($this->conn) {
			db2_rollback($this->conn);
		}
	}
	public function query($qStr) {
		if ($this->conn) {
			$result = db2_exec($this->conn, $qStr);
			return $result;
		}
	}
	public function prepareExec($sql, $arrays) {
		if ($this->conn) {
			$this->pstmt = db2_prepare($this->conn, $sql);
			return db2_execute($this->pstmt, $arrays);
		}
	}
	//
	// XML関連拡張機能
	//
	public function xquery($xqStr, $targetSXML) {
		if ($this->conn) {
			$result = db2_exec($this->conn, $xqStr);
			while ($row = db2_fetch_array($result)) {
				sxml_append($targetSXML, simplexml_load_string($row[0]));
			}
		}
	}
	public function prepareXQuery($xqStr, $Xarrays, $targetSXML) {
		if ($this->conn) {
			$result = $this->prepareExec($xqStr, $Xarrays);
			while ($row = db2_fetch_array($result)) {
				sxml_append($targetSXML, simplexml_load_string($row[0]));
			}
		}
	}
	public function xmlUpdate($tblName, $xid, $xmlStr) {
		$update = "update $tblName set data=? where id=?";
		$pparray[] = $xmlStr;
		$pparray[] = $xid;
		return $this->prepareExec($update, $pparray);
	}
}
